<script setup >
import { ref } from "vue";
import { ElMessage } from "element-plus";
import "element-plus/theme-chalk/el-message.css";
// import type { TabsPaneContext } from 'element-plus'
// import { sendcodeData, loginByUsername, loginByPhone } from "@/apis/login"
import { sendcodeData } from "@/apis/login"
import { userUserStore } from '@/stores/user'
import { useRouter } from "vue-router";

const userStore = userUserStore()
const router = useRouter()
const activeName = ref("username");
const usernameRuleFormRef = ref(null);
const usernameRuleForm = ref({
  name: "",
  password: "",
  agree: false,
});
const usernameRules = {
  name: [
    { required: true, message: "请输入登入名称", trigger: "blur" },
    { min: 8, max: 16, message: "长度在 8 到 16 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" },
  ],
  agree: [
    {
      validator: (rule, value, callback) => {
        // console.log(value)
        if (!value) {
          return callback(new Error("请勾选协议"));
        }else{
          callback()
        }
      },
    },
  ],
};
const phoneRuleFormRef = ref(null);
const phoneRuleForm = ref({
  phone: "",
  password: "",
  agree: false,
});
const phoneRules = {
  phone: [
    { required: true, message: "请输入手机号", trigger: "blur" },
    { min: 11, max: 11, message: "长度在 11 个字符", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        // console.log(value)
        if (!phoneTest.value.test(value)) {
          return callback(new Error("不符合手机号"));
        }else{
          callback()
        }
      },
    },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 6, message: "长度在 6 个字符", trigger: "blur" },
  ],
  agree: [
    {
      validator: (rule, value, callback) => {
        // console.log(value)
        if (!value) {
          return callback(new Error("请勾选协议"));
        }else{
          callback()
        }
      },
    },
  ],
};
const totalSecond = ref(2);
const second = ref(2);
const butsty = ref("primary");
const timmer = ref(null);
const phoneTest = ref(/^1[3456789]\d{9}$/);

const handleClick = () => {
  //   console.log(activeName.value);
  if (activeName.value === "phone") {
    usernameRuleFormRef.value.resetFields();
  } else {
    phoneRuleFormRef.value.resetFields();
  }
};

const sendCode = async() => {
  if (!phoneTest.value.test(phoneRuleForm.value.phone)) {
    ElMessage({ type: "error", message: "手机号不符合规范无法发送验证码" });
    return;
  }
  if (!timmer.value && second.value === totalSecond.value) {
    const res = await sendcodeData(phoneRuleForm.value.phone)
    ElMessage({ type: "success", message: res.data });
    butsty.value = "info";
    timmer.value = setInterval(() => {
      second.value--;
      if (second.value <= 0) {
        butsty.value = "primary";
        clearInterval(timmer.value);
        timmer.value = null;
        second.value = totalSecond.value;
      }
    }, 1000);
  }
};

const usernameSub = () => {
  usernameRuleFormRef.value.validate(async(valid) => {
    if(!valid) return false
    // const res = await loginByUsername(usernameRuleForm.value)
    // console.log(res.data)
    await userStore.getUserInfoByPassword(usernameRuleForm.value)
    ElMessage({ type: "success", message: '登入成功' });
    router.replace({path:'/'})
  });
};

const phoneSub = () => {
  phoneRuleFormRef.value.validate(async(valid) => {
    if(!valid) return false
    // const res = await loginByPhone(phoneRuleForm.value)
    // console.log(res.data)
    await userStore.getUserInfoByPhone(phoneRuleForm.value)
    ElMessage({ type: "success", message: '登入成功' });
    router.replace({path:'/'})
  });
};
</script>


<template>
  <div>
    <header class="login-header">
      <div class="container m-top-20">
        <h1 class="logo">
          <RouterLink to="/">小兔鲜</RouterLink>
        </h1>
        <RouterLink class="entry" to="/">
          进入网站首页
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
      </div>
    </header>
    <section class="login-section">
      <div class="wrapper">
        <!-- <nav>
          <a href="javascript:;">账户登录</a>
        </nav> -->

        <el-tabs
          type="border-card"
          class="demo-tabs"
          v-model="activeName"
          @tab-click="handleClick"
        >
          <el-tab-pane label="账户登录" name="username">
            <div class="account-box">
              <div class="form">
                <el-form
                  label-position="right"
                  label-width="60px"
                  status-icon
                  ref="usernameRuleFormRef"
                  :model="usernameRuleForm"
                  :rules="usernameRules"
                >
                  <el-form-item label="账户" prop="name">
                    <el-input v-model="usernameRuleForm.name" />
                  </el-form-item>
                  <el-form-item label="密码" prop="password">
                    <el-input v-model="usernameRuleForm.password" />
                  </el-form-item>
                  <el-form-item label-width="22px" prop="agree">
                    <el-checkbox v-model="usernameRuleForm.agree" size="large">
                      我已同意隐私条款和服务条款
                    </el-checkbox>
                  </el-form-item>
                  <el-button size="large" class="subBtn" @click="usernameSub"
                    >点击登录</el-button
                  >
                </el-form>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="手机号登录" name="phone">
            <div class="account-box">
              <div class="form">
                <el-form
                  label-position="right"
                  label-width="60px"
                  status-icon
                  ref="phoneRuleFormRef"
                  :model="phoneRuleForm"
                  :rules="phoneRules"
                >
                  <el-form-item prop="phone" label="手机号">
                    <el-input v-model="phoneRuleForm.phone" />
                    <el-button
                      class="sendcodebut"
                      @click="sendCode"
                      :type="butsty"
                      >{{
                        second === totalSecond
                          ? "获取验证码"
                          : "等" + second + "秒后重新获取"
                      }}</el-button
                    >
                  </el-form-item>
                  <el-form-item prop="password" label="验证码">
                    <el-input v-model="phoneRuleForm.password" />
                  </el-form-item>
                  <el-form-item label-width="22px" prop="agree">
                    <el-checkbox size="large" v-model="phoneRuleForm.agree">
                      我已同意隐私条款和服务条款
                    </el-checkbox>
                  </el-form-item>
                  <el-button size="large" class="subBtn" @click="phoneSub">点击登录</el-button>
                </el-form>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;">关于我们</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">售后服务</a>
          <a href="javascript:;">配送与验收</a>
          <a href="javascript:;">商务合作</a>
          <a href="javascript:;">搜索推荐</a>
          <a href="javascript:;">友情链接</a>
        </p>
        <p>CopyRight &copy; 小兔鲜儿</p>
      </div>
    </footer>
  </div>
</template>

<style scoped lang='scss'>
.sendcodebut {
  margin-top: 10px;
  width: 130px;
}
.login-header {
  background: #fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 132px;
      width: 100%;
      text-indent: -9999px;
      background: url("@/assets/images/logo.png") no-repeat center 18px /
        contain;
    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: $xtxColor;
      letter-spacing: -5px;
    }
  }
}

.login-section {
  background: url("@/assets/images/login-bg.png") no-repeat center / cover;
  height: 488px;
  position: relative;

  .wrapper {
    width: 380px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 54px;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~ a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {
      color: $xtxColor;

      i {
        font-size: 14px;
      }
    }
  }

  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

          &.error {
            border-color: $priceColor;
          }

          &.active,
          &:focus {
            border-color: $xtxColor;
          }
        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: $priceColor;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: $xtxColor;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
  background: $xtxColor;
  width: 100%;
  color: #fff;
}
</style>